package com.xingdata.jjxc;

import java.io.IOException;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.util.Log;

import com.iflytek.cloud.SpeechUtility;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.xingdata.jjxc.ar_navi.ARHudComm;
import com.xingdata.jjxc.ar_navi.DeviceSetting;
import com.xingdata.jjxc.ar_navi.Sysconfig;
import com.xingdata.jjxc.pushnavi.UdpClient;
import com.xingdata.jjxc.utils.CrashHandler;

/**
 * Java中全局变量，一般定义public static类型的变量。此方法不符合Android框架构架，Android中使用Application
 * context。 Application是一个基类，作用：获取整个App的状态 public class App extends Application
 * {}
 * 
 * @author ysc
 * 
 */
public class App extends Application {
	// modified by ly
    public static boolean isS3Pruduct = true;
	public static App instance;
	public static DeviceSetting deviceSettings;
	public Activity mainActivity;
	public static final String SEGMENTSOURCE = "http://jjfile.51zzd.com";
	// 接口
	// public static final String SEGMENT1 = "http://hud.irishud.com.cn";
	/** 域名 */
	public static final String SEGMENT1 = "http://jjxc.hudim.com";
	/** 请求方法: 域名 + /index.php/Hud/Api/ + 方法 */
	public static final String SEGMENT2 = "/index.php/Hud/Api/";

	/** jjxc条款信息链接 */
	public static final String JJ_AGREEMENT = "https://jjxc.hudim.com/soft/JJXC_Plugin/JJXCAgreement.html";

	/** 版本升级 */
	public static final String ZZD_REQUEST_CHECKVERSION = SEGMENT1 + SEGMENT2 + "System/softupdate";

	// public static final String ZZD_VERSION_UPGRADE =
	// "http://www.irishud.com.cn/soft/jjxc/dis/android/JJXC_Release.apk";
	// public static final String ZZD_VERSION_UPGRADE =
	// "http://jjxc.hudim.com/soft/JJXC_Release.apk";
	/** 1.注册发送验证码 */
	public static final String ZZD_REQUEST_SENDCODE = SEGMENT1 + "/index.php/Hud/Public/sendsms";
	/** 2. 注册校验验证码 */
	public static final String ZZD_REQUEST_CHECKCODE = SEGMENT1 + "/index.php/Hud/Public/checksms";

	/** 3.注册 */
	public static final String ZZD_REQUEST_REGISTER = SEGMENT1 + SEGMENT2 + "register";

	/** 6.忘记密码 */
	public static final String ZZD_REQUEST_FORGETPWD = SEGMENT1 + SEGMENT2 + "forgetpwd";
	/** 8.登录 */
	public static final String ZZD_REQUEST_LOGIN = SEGMENT1 + SEGMENT2 + "login";

	/** 7.检查sn */
	public static final String zzd_request_checksn = SEGMENT1 + SEGMENT2 + "System/checksn";
	// 8.登录获取用户更多信息
	// public static final String ZZD_REQUEST_USEREXTRA = SEGMENT1 + SEGMENT2
	// + "User/getother";
	
	/** 9.修改密码*/
	public static final String ZZD_REQUEST_UPDATEUSERPWD = SEGMENT1 + SEGMENT2 + "updateuserpwd";
	
	/** 修改昵称 */
	public static final String ZZD_REQUEST_UPDATEUSER = SEGMENT1 + SEGMENT2 + "updateuser";
	// 9.修改用户密码、公司名称、头像、库存预警、意见反馈
	/**
	 * 修改用户密码 修改公司名称 修改头像 库存预警设置 意见反馈
	 */
	public static final String ZZD_REQUEST_CHANGEPWD = SEGMENT1 + SEGMENT2 + "updateuser";

	/** 意见反馈 */
	public static final String ZZD_REQUEST_FEEFBACK = SEGMENT1 + SEGMENT2 + "feedback";
	/** 点赞 */
	public static final String ZZD_REQUEST_ASSIST = SEGMENT1 + SEGMENT2 + "support";
	/** 10.上传图片，修改头像 */
	public static final String ZZD_REQUEST_UPLOAD_IMG = SEGMENT1 + "/index.php/Hud/Public/uploadimg";
	/** 11.绑定hud */
	public static final String ZZD_REQUEST_CARHUD = SEGMENT1 + SEGMENT2 + "Car/addhud";
	/** 12.用户常用地址 */
	public static final String ZZD_REQUEST_SITELIST = SEGMENT1 + SEGMENT2 + "Site/getsitelist";
	/** 13.用户常用地址-增，删 */
	public static final String ZZD_REQUEST_CHANGENAME = SEGMENT1 + SEGMENT2 + "Site/editsite";
	/** 13.获取单双号限行 */
	public static final String ZZD_REQUEST_RESTRICT = SEGMENT1 + SEGMENT2 + "System/restrict";
	/** 获取七牛Token */
	public static final String ZZD_REQUEST_GETUPLOADTOKEN = SEGMENT1 + SEGMENT2 + "getuploadtoken";
	/** 添加发现-提交参数 */
	public static final String ZZD_REQUEST_ADDDISCOVER = SEGMENT1 + SEGMENT2 + "adddiscover";
	/** 找车 */
	public static final String ZZD_REQUEST_FINDLOCATION = SEGMENT1 + SEGMENT2 + "findlocation";
	/* 39.导航收藏夹 */
	/** 39.导航收藏夹-添加收藏POST */
	public static final String ZZD_REQUEST_ADDCOLLECTION = SEGMENT1 + "/index.php/Hud/Navi/addCollection";
	/** 39.导航收藏夹-取消收藏POST */
	public static final String ZZD_REQUEST_CANCELCOLLECTION = SEGMENT1 + "/index.php/Hud/Navi/cancelCollection";
	/** 39.导航收藏夹-获取数据收藏GET */
	public static final String ZZD_REQUEST_FINDCOLLECTIONSBYUSERID = SEGMENT1 + "/index.php/Hud/Navi/findCollectionsbyUserid";

	/**
	 * 获取车辆品牌列表
	 */
	public static String ZZD_REQUEST_GETCARBRANCHLIST = SEGMENT1 + SEGMENT2 + "getcarbranchlist";
 
	/**
	 * 获取车辆号列表 getcarmodellist->
	 */
	public static String ZZD_REQUEST_GETCARMODELLIST = SEGMENT1 + SEGMENT2 + "getcarmodellist";

	/* 爱车 */
	/** 添加爱车信息 */
	public static String ZZD_REQUEST_ADDCAR = SEGMENT1 + SEGMENT2 + "addcar";
	/** 更改爱车信息 */
	public static String ZZD_REQUEST_UPDATECAR = SEGMENT1 + SEGMENT2 + "updatecar";
	/** 爱车列表 */
	public static String ZZD_REQUEST_GETCARLIST = SEGMENT1 + SEGMENT2 + "getcarlist";
	/** 删除爱车信息 */
	public static String ZZD_REQUEST_DELCAR = SEGMENT1 + SEGMENT2 + "delcar";

	/** 获取HUD版本号 */
	public static final String ZZD_REQUEST_GETHUDVERSION = SEGMENT1 + "/index.php/Hud/public/upgrade";
	/** 发现列表 */
	public static String ZZD_REQUEST_GETDISCOVERLIST = SEGMENT1 + SEGMENT2 + "getdiscoverlist";

	/**
	 * 驾驶习惯
	 */
	public static String ZZD_REQUEST_FINDOBDDRIVER = SEGMENT1 + SEGMENT2 + "findobdDriver";
	/**
	 * 驾驶习惯
	 */
	public static String ZZD_REQUEST_FINDOBDTRIP = SEGMENT1 + SEGMENT2 + "findobdTrip";

	/**
	 * 发现列表
	 */
	public static String ZZD_REQUEST_FINDOBDCURRENT = SEGMENT1 + SEGMENT2 + "findobdCurrent";
	/**
	 * 评论列表
	 */
	public static String ZZD_REQUEST_COMMENTLIST = SEGMENT1 + SEGMENT2 + "commentlist";
	/**
	 * 添加评论
	 */
	public static String ZZD_REQUEST_ADDCOMMENT = SEGMENT1 + SEGMENT2 + "addcomment";
	/**
	 * 七牛域名
	 */
	public static final String DOMAINNAME_QINIU = "irishud.com.cn/";

	// 更新安装�??
	public static final String ZZD_DOWNLOAD = SEGMENT1 + "/soft/zzd4.1.apk";

	// 微信�??放平台ID
	public static final String APP_ID = "wxf13b68f787bee7e8";
	/** HUD简介 **/
	public static final String ABOUT_APP_URL = "http://www.hudim.com/Home/Intro/x1.html";
	// 下拉刷新 - 0 | 上拉加载 - 1
	public static final int METHOD_REFRESH = 0;
	public static final int METHOD_LOADMORE = 1;
	public static final int METHOD_INIT = 2;
	public static ImageLoader imageLoader;
	public static DisplayImageOptions options;

	/** 来电 */
	public static final int CommingCall = 1;
	/** 去电 */
	public static final int GoingCall = 2;
	/** 错误 */
	public static final int Error = 0;
	/** 来电去电弹屏 */
	public static final String CommingShowView = "1";
	/** 通话结束弹屏 */
	public static final String EndShowView = "5";
	/** 陌生号码弹屏 */
	public static final String unFamiliarNumber = "6";
	/** 不通话直接挂断 */
	public static final String HangupTelephone = "3";
	/** 来电接通电话 */
	public static final String Through = "4";

	/** 直接挂断 */
	public static final int Come_Hang_Up = 7;
	/** 通话后结束 */
	public static final int Come_Through_Finish = 10;
	/** 直接退出 */
	public static final int Come_Quit = 8;
	/** 接通电话 */
	public static final int Come_Through = 9;
	/** 最小剩余空间 */
	public static final long Min_Memory = 30720;
	/** 去电响铃 */
	public static final int GoRingDown = 11;
	/** 来电响铃 */
	public static final int ComeRingDown = 6;

	/**手机导航相关设置**/
	Sysconfig naviConfig;

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("APP", "启动App");

		// 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		// SDKInitializer.initialize(this);
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());

		instance = this;

		initImageLoader(getApplicationContext());
		initImageLoaderOptions();
		// configWeChat(getApplicationContext());

		// 应用程序入口处调用,避免手机内存过小,杀死后台进程后通过历史intent进入Activity造成SpeechUtility对象为null
		// 如在Application中调用初始化，需要在Mainifest中注册该Applicaiton
		// 注意：此接口在非主进程调用会返回null对象，如需在非主进程使用语音功能，请增加参数：SpeechConstant.FORCE_LOGIN+"=true"
		// 参数间使用“,”分隔。
		// 设置你申请的应用appid
		SpeechUtility.createUtility(this, "appid=" + getString(R.string.app_id));
		// modified by ly
        if(isS3Pruduct){
        	try {
				new UdpClient();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }

		naviConfig = new Sysconfig(this); // 手机端导航的配置数据
		deviceSettings = new DeviceSetting();
		deviceSettings.onBoardNavi = naviConfig.getOnBoardNaviMode();

		ARHudComm.getInstance().init();//lxs----------初始化 AR HUD UDP通信
		ARHudComm.getInstance().setNavi_data_debug_mode(naviConfig.getNaviDataDebugMode());

		// CrashReport.initCrashReport(getApplicationContext(), "900019504", false);
	}

	public static App getContext() {
		return instance;
	}

	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(config);

	}

	/** 界面“我的”头像设置资源 */
	public static void initImageLoaderOptions() {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.bg_user2)
				.showImageForEmptyUri(R.drawable.bg_user2).showImageOnFail(R.drawable.bg_user2).cacheInMemory(false)
				.cacheOnDisc(false).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
				.resetViewBeforeLoading(false).displayer(new FadeInBitmapDisplayer(100)).build();
	}

	// public static void configWeChat(Context context) {
	// UMWXHandler wxHandler = new UMWXHandler(context, APP_ID);
	// wxHandler.addToSocialSDK();
	// }

	/**
	 * 判断网络连接状况
	 * 
	 * @param
	 * @return 状态
	 */
	public boolean isNetworkConnected() {

		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		cwjManager.getActiveNetworkInfo();
		boolean netSataus = true;
		if (cwjManager.getActiveNetworkInfo() != null) {

			netSataus = cwjManager.getActiveNetworkInfo().isAvailable();

		}
		return netSataus;
	}
}
